package me.flyray.crm.core.biz;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.crm.request.CertificationRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.enums.CertificationStatus;
import com.github.wxiaoqi.security.common.enums.CertificationType;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;
import me.flyray.crm.core.entity.CustomerBase;
import me.flyray.crm.core.entity.PersonalBase;
import me.flyray.crm.core.entity.PersonalCertificationInfo;
import me.flyray.crm.core.mapper.CustomerBaseMapper;
import me.flyray.crm.core.mapper.PersonalBaseMapper;
import me.flyray.crm.core.mapper.PersonalCertificationInfoMapper;

/**
 * 用户实名认证
 *
 * @author centerroot
 * @email ${email}
 * @date 2018-09-10 15:02:35
 */
@Service
@Slf4j
public class PersonalCertificationInfoBiz extends BaseBiz<PersonalCertificationInfoMapper, PersonalCertificationInfo> {
	@Autowired
	private PersonalBaseMapper personalBaseMapper;
	@Autowired
	private CustomerBaseMapper customerBaseMapper;
	
	@Autowired
	private PersonalCertificationInfoMapper personalCertificationInfoMapper;
	/**
	 * 用户实名认证
	 * @author centerroot
	 * @time 创建时间:2018年9月8日下午4:47:04
	 * @param 
	 * @return
	 */
	public Map<String, Object> certification(CertificationRequest certificationRequest){
		log.info("【 用户实名认证】   请求参数：{}",EntityUtils.beanToMap(certificationRequest));
		Map<String, Object> respMap = new HashMap<String, Object>();
		try {
			PersonalBase queryPerParam=new PersonalBase();
			queryPerParam.setPlatformId(certificationRequest.getPlatformId());
			queryPerParam.setCustomerId(certificationRequest.getCustomerId());
			PersonalBase personalBase=personalBaseMapper.selectOne(queryPerParam);
			if(null==personalBase){
				respMap.put("code", BizResponseCode.PER_NOTEXIST.getCode());
			    respMap.put("message", BizResponseCode.PER_NOTEXIST.getMessage());
			    return respMap;
			}
			
			PersonalCertificationInfo querycertificationInfo =new PersonalCertificationInfo();
			querycertificationInfo.setPlatformId(certificationRequest.getPlatformId());
			querycertificationInfo.setAuthType(certificationRequest.getAuthType());
			querycertificationInfo.setPersonalId(personalBase.getPersonalId());
			PersonalCertificationInfo personalCert=personalCertificationInfoMapper.selectOne(querycertificationInfo);
			//用户实名认证信息表不为空
			if(null!=personalCert){
				//1.只要第三方认证了，不管认证类型是什么，都返回已认证。
				if(null!=personalCert.getAuthId()){
					respMap.put("code", BizResponseCode.PER_CERTIFICATION_EXIST.getCode());
					respMap.put("message", BizResponseCode.PER_CERTIFICATION_EXIST.getMessage());
					return respMap;					
				}
				//2.四要素已认证，未进行第三方认证，请求认证类型为四要素认证的时候，返回已认证。
				if(null==personalCert.getAuthId()&&CertificationType.FOURFACTORS_CERTIFY.getCode().equals(certificationRequest.getAuthType())){
					respMap.put("code", BizResponseCode.PER_CERTIFICATION_EXIST.getCode());
					respMap.put("message", BizResponseCode.PER_CERTIFICATION_EXIST.getMessage());
					return respMap;	
				}
			}
			String id = String.valueOf(SnowFlake.getId()); 
			PersonalCertificationInfo  certificationInfo=new PersonalCertificationInfo();
			if(CertificationType.FOURFACTORS_CERTIFY.getCode().equals(certificationRequest.getAuthType())){//四要素的实名认证
				if(CertificationStatus.SUCCESS_CERTIFY.getCode().equals(personalBase.getAuthenticationStatus())){//认证成功
					respMap.put("code", BizResponseCode.PER_CERTIFICATION_EXIST.getCode());
			        respMap.put("message", BizResponseCode.PER_CERTIFICATION_EXIST.getMessage());
			        return respMap;
				}
				certificationInfo.setId(id);
				certificationInfo.setPlatformId(certificationRequest.getPlatformId());
				certificationInfo.setPersonalId(personalBase.getPersonalId());
				certificationInfo.setBankCardNo(certificationRequest.getBindCardTailno());
				certificationInfo.setBindEncryptCardNo(certificationRequest.getBankCardNo());
				certificationInfo.setBindCardMobile(certificationRequest.getBindCardMobile());
				certificationInfo.setBankNo(certificationRequest.getBankNo());
				certificationInfo.setBankName(certificationRequest.getBankName());
				certificationInfo.setSubbranchNo(certificationRequest.getSubbranchNo());
				certificationInfo.setSubbranchName(certificationRequest.getSubbranchName());
			}else if(CertificationType.TAHUA_THIRD_CERTIFY.getCode().equals(certificationRequest.getAuthType())){//泰华实名认证
				certificationInfo.setId(id);
				certificationInfo.setPlatformId(certificationRequest.getPlatformId());
				certificationInfo.setPersonalId(personalBase.getPersonalId());
				certificationInfo.setAuthId(certificationRequest.getAuthId());
			}

			certificationInfo.setAuthType(certificationRequest.getAuthType());
			certificationInfo.setCreateTime(new Date());
			personalCertificationInfoMapper.insertSelective(certificationInfo);
			//往个人基础信息表中存入姓名和身份证号并更新认证状态
			personalBase.setAuthenticationStatus("02"); // 认证成功
			personalBase.setIdCard(certificationRequest.getIdCard());
			personalBase.setRealName(certificationRequest.getRealName());
			personalBaseMapper.updateByPrimaryKey(personalBase);
			
			respMap.put("code", BizResponseCode.OK.getCode());
			respMap.put("message", BizResponseCode.OK.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			log.info("用户实名认证-报错。。。。。。{}"+e.getMessage());
			respMap.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			respMap.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
		}
		return respMap;
		
	}
	
	/**
	 * 实名认证状态查询
	 * @author centerroot
	 * @time 创建时间:2018年9月25日上午9:50:56
	 * @param certificationRequest
	 * @return
	 */
	public Map<String, Object> realNameQuery(CertificationRequest certificationRequest){
		log.info("【 查询实名认证状态】   请求参数：{}",EntityUtils.beanToMap(certificationRequest));
		Map<String, Object> respMap = new HashMap<String, Object>();
		PersonalCertificationInfo certificationInfoReq = new PersonalCertificationInfo();
		BeanUtils.copyProperties(certificationRequest, certificationInfoReq);
		CustomerBase queryCusParam=new CustomerBase();
		PersonalBase queryPerParam=new PersonalBase();
		queryPerParam.setPlatformId(certificationRequest.getPlatformId());
		queryCusParam.setPlatformId(certificationRequest.getPlatformId());
		//20190102根据请求传过来的参数进行判断。
		if(!StringUtils.isEmpty(certificationRequest.getCustomerId())){
			queryPerParam.setCustomerId(certificationRequest.getCustomerId());	
			queryCusParam.setCustomerId(certificationRequest.getCustomerId());
		}
		if(!StringUtils.isEmpty(certificationRequest.getPersonalId())){
			queryPerParam.setPersonalId(certificationRequest.getPersonalId());	
			queryCusParam.setPersonalId(certificationRequest.getPersonalId());
		}
		if(StringUtils.isEmpty(certificationRequest.getCustomerId())&&StringUtils.isEmpty(certificationRequest.getPersonalId())){
			respMap.put("code", BizResponseCode.PERSONALBASE_NOTEXIST.getCode());
	        respMap.put("message", BizResponseCode.PERSONALBASE_NOTEXIST.getMessage());
	        return respMap;
		}
		
		PersonalBase personalBase=personalBaseMapper.selectOne(queryPerParam);
		if(null==personalBase){
			respMap.put("code", BizResponseCode.PER_NOTEXIST.getCode());
		    respMap.put("message", BizResponseCode.PER_NOTEXIST.getMessage());
			log.info("【 查询实名认证状态】   响应结果：{}",respMap);
		    return respMap;
		}
		certificationInfoReq.setPersonalId(personalBase.getPersonalId());
		List<PersonalCertificationInfo> certificationInfos = personalCertificationInfoMapper.select(certificationInfoReq);
		if(certificationInfos.size()<1 && "00".equals(personalBase.getAuthenticationStatus())){
			respMap.put("code", BizResponseCode.PER_CERTIFICATION_NOTEXIST.getCode());
		    respMap.put("message", BizResponseCode.PER_CERTIFICATION_NOTEXIST.getMessage());
			log.info("【 查询实名认证状态】   响应结果：{}",respMap);
		    return respMap;
		}
		//通过customer_base查询交易密码。
		CustomerBase customerBase=customerBaseMapper.selectOne(queryCusParam);
		if(null!=customerBase){
			//0-正常；1-未设置；2-锁定。
			respMap.put("payPasswordStatus", customerBase.getPayPasswordStatus());
			respMap.put("payPassword", customerBase.getPayPassword());
		}
		respMap.put("personalBase", personalBase);
		respMap.put("certificationInfos", certificationInfos);
		respMap.put("code", BizResponseCode.OK.getCode());
	    respMap.put("message", BizResponseCode.OK.getMessage());
		log.info("【 查询实名认证状态】   响应结果：{}",respMap);
		return respMap;
	}
	
}